﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_26_RemoveDuplicatesFromAnRderedArray
{
    //删除有序数组中的重复项
    //Complete 提供了一个思路，数组不提供删除功能，但是可以给后面的值对前面进行覆盖,来达到删除的目的
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] nums = new int[] { 1, 1, 1,2,3 };
      
            Console.WriteLine(RemoveDuplicates(nums));
            Console.ReadKey();
        }

        public static int RemoveDuplicates(int[] nums)
        {
            int index = 0;

            int valueIndex = 0;

            return Loop(nums, index, valueIndex);
        }

        public static int Loop(int[] nums,int index,int valueIndex)
        {
            if (index == nums.Length-1)
            {
                nums[valueIndex] = nums[nums.Length - 1];
                return valueIndex+1;
            }

            for (int i = index; i < nums.Length; i++)
            {
                if (i + 1>= nums.Length)
                {
                    nums[valueIndex] = nums[i];
                    valueIndex++;
                    index++;
                    return Loop(nums, index, valueIndex);
                }
                if (nums[i] == nums[i + 1])
                {
                    index++;
                }
                else
                {
                    nums[valueIndex] = nums[i];
                    valueIndex++;
                    index++;
                    return Loop(nums, index, valueIndex);
                }
            }
            return valueIndex;
        }
    }
}
